package abc.Tool;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter(urlPatterns = "/abc/*")
public class CoreFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //初始化函数
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain Chain) throws IOException, ServletException {
        // 执行拦截的逻辑代码的
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse)resp;

        //request.getRequestURI();//得到的是/wyl/login.jsp
        String uri = request.getRequestURI();
        if("/abc/login.jsp".equals(uri)||uri.endsWith(".css")){
            //此时被拦截的是登录器，直接允许通过
            Chain.doFilter(req,resp);
            return;
        }
        HttpSession session = request.getSession();
        Object uid = session.getAttribute("UID");
        if(uid==null){
            //说明没有登录
            response.sendRedirect("abc/login.jsp");
            return;
        }
        Chain.doFilter(req,resp);
    }

    @Override
    public void destroy() {
        //析构函数，当过滤器被销毁的时候执行
    }
}


